package com.ruoyi.produce.process.worksheet.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.produce.process.worksheet.domain.ProduceProcedureTransferSheet;
import com.ruoyi.produce.process.worksheet.domain.ProduceWorkSheet;
import com.ruoyi.produce.process.worksheet.domain.ProduceWorkSheetAllocationRecord;
import com.ruoyi.produce.process.worksheet.service.IProduceWorkSheetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 加工单信息
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.14
 */
@RestController
@RequestMapping("/produce/worksheet")
@Api(value = "加工单模块", tags = "加工单接口")
public class ProduceWorkSheetController extends BaseController {
    @Autowired
    private IProduceWorkSheetService produceWorkSheetService;


    /**
     * 获取加工单
     * @param produceWorkSheet
     * @return
     */
    @ApiOperationSupport(includeParameters = {"ProduceWorkSheet.produceId", "ProduceWorkSheet.workSheetId", "ProduceWorkSheet.planSelfId", "ProduceWorkSheet.createTime"})
    @ApiOperation(value = "获取加工单列表",notes = "查询所有加工单信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProduceWorkSheet produceWorkSheet){
        startPage();
        List<ProduceWorkSheet> produceWorkSheetList = produceWorkSheetService.selectProduceWorkSheetList(produceWorkSheet);
        return getDataTable(produceWorkSheetList);
    }

    /**
     * 获取正在生产的加工单
     * @param produceWorkSheet
     * @return
     */
    @ApiOperation(value = "获取正在生产的加工单列表",notes = "查询未完成的加工单信息")
    @PostMapping("/list/processing")
    public TableDataInfo selectProcessingProduceWorkSheetList(@RequestBody ProduceWorkSheet produceWorkSheet){
        startPage();
        List<ProduceWorkSheet> produceWorkSheetList = produceWorkSheetService.selectProcessingProduceWorkSheetList(produceWorkSheet);
        return getDataTable(produceWorkSheetList);
    }

    /**
     * 获取生产完成的加工单
     * @param produceWorkSheet
     * @return
     */
    @ApiOperation(value = "获取生产完成的加工单列表",notes = "查询生产完成的加工单信息")
    @PostMapping("/list/finished")
    public TableDataInfo selectFinishedProduceWorkSheetList(@RequestBody ProduceWorkSheet produceWorkSheet){
        startPage();
        List<ProduceWorkSheet> produceWorkSheetList = produceWorkSheetService.selectFinishedProduceWorkSheetList(produceWorkSheet);
        return getDataTable(produceWorkSheetList);
    }

    /**
     * 导出加工单
     * @param produceWorkSheet
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody ProduceWorkSheet produceWorkSheet)
    {
        List<ProduceWorkSheet> produceWorkSheetList = produceWorkSheetService.selectProduceWorkSheetList(produceWorkSheet);
        ExcelUtil<ProduceWorkSheet> util = new ExcelUtil<ProduceWorkSheet>(ProduceWorkSheet.class);
        return util.exportExcel(produceWorkSheetList, "加工单数据");
    }

    /**
     * 新增加工单
     * @param produceWorkSheet
     * @return
     */
    @ApiOperation(value = "新增加工单", notes = "新增加工单")
    @ApiImplicitParam(name = "produceWorkSheet", value = "新增加工单(参数：workSheetId，planSelfId，workSheetNumber，startTime，predictedFinishingTime，createById，remark)", required = true, dataType = "ProduceWorkSheet")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProduceWorkSheet produceWorkSheet){

        return toAjax(produceWorkSheetService.insertProduceWorkSheet(produceWorkSheet));
    }

    /**
     * 修改加工单
     * @param produceWorkSheet
     * @return
     */
    @ApiOperation(value = "修改加工单", notes = "修改加工单")
    @ApiImplicitParam(name = "produceWorkSheet", value = "修改生产计划(参数：workSheetNumber，startTime，predictedFinishingTime，remark)", dataType = "ProduceWorkSheet")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody ProduceWorkSheet produceWorkSheet)
    {
        return toAjax(produceWorkSheetService.updateProduceWorkSheet(produceWorkSheet));
    }

    /**
     * 删除加工单
     * 仅修改删除标志位
     * @param produceWorkSheet
     * @return
     */
    @ApiOperation(value = "删除加工单", notes = "删除加工单,仅删除标志位")
    @ApiImplicitParam(name = "produceWorkSheet", value = "删除加工单", dataType = "ProduceWorkSheet")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ProduceWorkSheet produceWorkSheet)
    {
        return toAjax(produceWorkSheetService.updateProduceWorkSheetDeleteFlag(produceWorkSheet));
    }

    /**
     * 加工单产线分配
     * @param produceWorkSheet
     * @return
     */
    @PostMapping("/allocation")
    public AjaxResult allocation(@RequestBody ProduceWorkSheet produceWorkSheet)
    {
        return toAjax(produceWorkSheetService.updateProduceWorkSheetAllocation(produceWorkSheet));
    }

    /**
     * 加工单产线分配移除
     * @param produceWorkSheet
     * @return
     */
    @PostMapping("/removeAllocation")
    public AjaxResult removeAllocation(@RequestBody ProduceWorkSheet produceWorkSheet)
    {
        return toAjax(produceWorkSheetService.removeProduceWorkSheetAllocation(produceWorkSheet));
    }

    /**
     * 获取加工单产线分配记录
     * @param produceWorkSheetAllocationRecord
     * @return
     */
    @PostMapping("/listAllocation")
    public TableDataInfo listAllocation(@RequestBody ProduceWorkSheetAllocationRecord produceWorkSheetAllocationRecord){
        startPage();
        List<ProduceWorkSheetAllocationRecord> produceWorkSheetAllocationRecordList = produceWorkSheetService.selectProduceWorkSheetAllocationRecordList(produceWorkSheetAllocationRecord);
        return getDataTable(produceWorkSheetAllocationRecordList);
    }

    /**
     * 导出加工单产线分配记录
     * @param produceWorkSheetAllocationRecord
     * @return
     */
    @PostMapping("/exportAllocation")
    public AjaxResult exportAllocation(@RequestBody ProduceWorkSheetAllocationRecord produceWorkSheetAllocationRecord)
    {
        List<ProduceWorkSheetAllocationRecord> produceWorkSheetAllocationRecordList = produceWorkSheetService.selectProduceWorkSheetAllocationRecordList(produceWorkSheetAllocationRecord);
        ExcelUtil<ProduceWorkSheetAllocationRecord> util = new ExcelUtil<ProduceWorkSheetAllocationRecord>(ProduceWorkSheetAllocationRecord.class);
        return util.exportExcel(produceWorkSheetAllocationRecordList, "加工单产线分配数据");
    }

    /**
     * 获取加工单工序移交
     * @param produceProcedureTransferSheet
     * @return
     */
    @PostMapping("/listProcedureTransferSheet")
    public TableDataInfo listProcedureTransferSheet(@RequestBody ProduceProcedureTransferSheet produceProcedureTransferSheet){
        startPage();
        List<ProduceProcedureTransferSheet> produceProcedureTransferSheetList = produceWorkSheetService.selectProduceProcedureTransferSheetList(produceProcedureTransferSheet);
        return getDataTable(produceProcedureTransferSheetList);
    }

    /**
     * 新增/修改加工单工序移交
     * @param produceProcedureTransferSheet
     * @return
     */
    @PostMapping("/editProcedureTransferSheet")
    public AjaxResult editProcedureTransferSheet(@RequestBody ProduceProcedureTransferSheet produceProcedureTransferSheet)
    {
        if (produceProcedureTransferSheet.getTransferNumber() < 0){
            return AjaxResult.error("工序移交数量不可小于0！");
        }
        return toAjax(produceWorkSheetService.editProduceProcedureTransferSheet(produceProcedureTransferSheet));
    }

    /**
     * 确认加工单工序移交
     * @param produceProcedureTransferSheet
     * @return
     */
    @PostMapping("/confirmProcedureTransferSheet")
    public AjaxResult confirmProcedureTransferSheet(@RequestBody ProduceProcedureTransferSheet produceProcedureTransferSheet)
    {
        return toAjax(produceWorkSheetService.confirmProduceProcedureTransferSheet(produceProcedureTransferSheet));
    }
}
